/*
 *  admissions.js
 *
 *  A collection of methods for the Domain Knowledge specific to the
 *  Admissions Unit.  These methods are all centralized in this location
 *  instead of diffused throughout the application so that one can
 *  reason about this Domain Model more easily.
 *
 *  @author Tanya L. Crenshaw
 */
define([], function() 
{
	highSchoolCeebs = new Array();
	highSchoolNames = new Array();
	
	/*
	 * regions()
	 *
	 * An object that represents all of the region assignments in the
	 * Admissions Unit.  Each property is a region name.  Each value
	 * of each property is an array.  The first element of the array
	 * is the two-letter state abbreviation associated with the region.
	 * The remainder of the array is any subregions for the region.
	 *
	 * TODO: Incomplete list.  I don't feel, either, that this is the
	 * best way to organize this data.
	 */
	var regions = 
	{
		"Alaska" : ["AK"],
		"Arizona" : ["AZ"],
		"California" : ["CA"],
		"Colorado" : ["CO"],
		"Hawaii" : ["HI"],
		"Idaho" : ["ID"],
		"Illinois" : ["IL"],
		"Maine" : ["ME"],
		"Minnesota" : ["MN"],
		"Nebraska" : ["NE"],
		"New Mexico" : ["NM"],
		"New York" : ["NY"],
		"Nevada" : ["NV"],
		"Oregon" : ["OR"],
		"Texas" : ["TX"],
		"Utah" : ["UT"],
		"Washington" : ["WA"]
	};

	/*
	 * getRegions()
	 *
	 * Based on the "Counselor Assignments" provided by Jason McDonald, this
	 * method supplies an array of all of the Region Names for the Admissions
	 * Unit, as group by counselor.
	 *
	 * TODO: TLC.  Currently, this is only a partial list, as I am doing a
	 * proof of concept with regards to drop-down menus.  I need to finish
	 * enumerating all the regions.
	 *
	 * @param none
	 * @return regions An object containing each region as a property and
	 * an array of all subregions as the value of each property.
	 *
	 */
	var getRegions = function() 
	{
		return regions;
	};
	
	/*
	 * convertRegionToState()
	 *
	 * Convert the possible menu choices available to a two-letter
	 * state abbreviation.
	 *
	 * @param menuChoice A user-supplied menu choice extracted from an HTML
	 * element.
	 * @return state A two-letter state abbreviation.
	 */
	var convertRegionToState = function(region) 
	{
		var state = regions[region][0];

		return state;
	};
	
	/*
	 * getHighSchoolName()
	 *
	 * Based on the construction of the Fusion Table used to supply high
	 * school address and CEEB information, this function extracts
	 * the high school name from a Fusion Table response and returns
	 * it as a string.
	 *
	 */
	var getHighSchoolName = function(response) 
	{
		return response["rows"][0][0].toString();
	};
	
	/*
	 * getHighSchoolAddress()
	 *
	 * Based on the construction of the Fusion Table used to supply high
	 * school address and CEEB information, this function extracts
	 * the high school address from a Fusion Table response and returns
	 * it as a string.
	 *
	 */
	var getHighSchoolAddress = function(response) 
	{
		return response["rows"][0][1].toString();
	};
	
	/*
	 * getZipcode()
	 *
	 * Based on the construction of the Fusion Table used to supply high
	 * school address and CEEB information, this function extracts
	 * the high school zipcode from a Fusion Table response and returns
	 * it as a string.
	 *
	 */
	var getZipcode = function(response) 
	{
		return response["rows"][0][4].toString();
	};
	
	
	/*
	 * ceebToHighSchool()
	 *
	 * This method takes a high school's ceeb and returns it's corresponding name
	 *
	 *  @param ceeb The ceeb of the high school to look up in the hash 
	 */
	var ceebToHighSchool = function(ceeb)
	{
		return highSchoolNames[ceeb];
	};
	
	
	/*
	 * highSchoolToCeeb()
	 *
	 * This method takes a high school's name and returns it's corresponding ceeb
	 *
	 *  @param name The name of the high school to look up in the hash 
	 */
	var highSchoolToCeeb = function(name)
	{
		//console.log(name);
		name = name.toUpperCase();
		for(var ceeb = 0; ceeb < highSchoolCeebs.length; ceeb++)
		{
			nextCeeb = highSchoolCeebs[ceeb];
			//console.log(highSchoolNames[nextCeeb]);
			if(highSchoolNames[nextCeeb] == name)
			{
				//console.log(nextCeeb);
				return nextCeeb;
			}
		}
		return "Ceeb not Found";
	};
	
	/*
	 * createCeebHash()
	 *
	 * This method takes all of the ceebs and high schools in the google fusion table
	 * from the initialization of usmap.js and makes a hash table out of it to save
	 * time during lookups later
	 * 
	 * @param data All of the high school names and ceebs in the google fusion table in usmap
	 *
	 */
	var createCeebHash = function(data)
	{
		for(var row = 0; row < data.rows.length; row++)
		{
			nextCeeb = data.rows[row][0];
			nextName = data.rows[row][1];
			nextName = nextName.toUpperCase();
			highSchoolNames[nextCeeb] = nextName;
			highSchoolCeebs.push(nextCeeb);
			//console.log(nextCeeb);
			//console.log(data.rows[row][1]);
		}
		
	};
	
	// Any functions defined in this return statement are considered public
	// functions by RequireJS, and accessible through the namespace that
	// is attached to this module when loaded in other files.
	return {
		getRegions : getRegions,
		convertRegionToState : convertRegionToState,
		getHighSchoolName : getHighSchoolName,
		getHighSchoolAddress : getHighSchoolAddress,
		getZipcode : getZipcode,
		ceebToHighSchool : ceebToHighSchool,
		highSchoolToCeeb : highSchoolToCeeb,
		createCeebHash : createCeebHash
	};
});